package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IYcDataService;
//import com.sun.org.apache.bcel.internal.generic.DCONST;
//import org.apache.poi.ss.usermodel.IconMultiStateFormatting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import javax.validation.Validator;
/**
 * 油厂数据管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@Service
public class YcDataServiceImpl implements IYcDataService
{
    @Autowired
    private YcDataMapper ycDataMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private YcOrgMapper ycOrgMapper;
    @Autowired
    private YcIndeMapper ycIndeMapper;

    @Autowired
    private YcMeasuresMapper ycMeasuresMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询油厂数据管理
     *
     * @param dataId 油厂数据管理主键
     * @return 油厂数据管理
     */
    @Override
    public YcData selectYcDataByDataId(Long dataId)
    {
        return ycDataMapper.selectYcDataByDataId(dataId);
    }

    /**
     * 查询油厂数据管理列表
     *
     * @param ycData 油厂数据管理
     * @return 油厂数据管理
     */
    @Override
    public List<YcData> selectYcDataList(YcData ycData)
    {
        return ycDataMapper.selectYcDataList(ycData);
    }

    /**
     * 查询油厂数据管理列表 (导出专用)
     *
     * @param ycData 油厂数据管理
     * @return 油厂数据管理集合
     */
    @Override
    public List<YcData> selectYcDataListUseExport(YcData ycData){
        List<YcData> ycDatas = ycDataMapper.selectYcDataList(ycData);
        for (YcData ycDataUseExport : ycDatas){
            if(ycIndeMapper.selectYcIndeByIndeId(ycDataUseExport.getIndeId()) != null){
                YcInde ycInde = ycIndeMapper.selectYcIndeByIndeId(ycDataUseExport.getIndeId());
                if(sysDeptMapper.selectDeptById(ycInde.getDeptId()) != null){
                    SysDept sysDept = sysDeptMapper.selectDeptById(ycInde.getDeptId());
                    ycDataUseExport.setDeptName(sysDept.getDeptName());
                }
                ycDataUseExport.setIndeIdName(ycInde.getIndeName());
                ycDataUseExport.setIndeLevel(ycInde.getIndeLevel());
            }
            if(ycOrgMapper.selectYcOrgByOrgId(ycDataUseExport.getOrgId()) != null){
                YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgId(ycDataUseExport.getOrgId());
                ycDataUseExport.setOrgIdName(ycOrg.getOrgName());
            }
        }
        return ycDatas;
    }

    /**
     * 查询更多的油厂数据管理列表
     *
     * @param ycData_v2 油厂数据管理
     * @return 油厂数据管理
     */
    @Override
    public List<YcData> selectYcDataListMore(YcData_v2 ycData_v2)
    {
        return ycDataMapper.selectYcDataListMore(ycData_v2);
    }
    /**
     * 新增油厂数据管理
     *
     * @param ycData 油厂数据管理
     * @return 结果
     */
    @Override
    public int insertYcData(YcData ycData)
    {
        ycData.setCreateTime(DateUtils.getNowDate());
        return ycDataMapper.insertYcData(ycData);
    }

    /**
     * 修改油厂数据管理
     *
     * @param ycData 油厂数据管理
     * @return 结果
     */
    @Override
    public int updateYcData(YcData ycData)
    {
        return ycDataMapper.updateYcData(ycData);
    }

    /**
     * 批量修改油厂措施管理
     *
     * @param dataIds 油厂措施管理
     */
    public void updateYcDataByDataIds(Long[] dataIds){
        for (Long dataId : dataIds){
            YcData ycData = ycDataMapper.selectYcDataByDataId(dataId);
            YcInde ycInde = ycIndeMapper.selectYcIndeByIndeId(ycData.getIndeId());
            if(ycData.getDataValue().compareTo(ycInde.getIndeFlag()) == -1 && ycInde.getRaiseLower() == 0){
                YcMeasures ycMeasures =new YcMeasures();
                ycMeasures.setDataId(ycData.getDataId());
                ycMeasures.setIndeId(ycData.getIndeId());
                ycMeasures.setMeaStatus(0);
                ycMeasuresMapper.insertYcMeasures(ycMeasures);
                YcMeasures ycMeasures1 = ycMeasuresMapper.selectYcMeasuresByDataId(ycData.getDataId());
                ycData.setStatus(3);
                ycData.setUpToStandard(1);
                ycData.setMeaId(ycMeasures1.getMeaId());
                ycDataMapper.updateYcData(ycData);
            }else if(ycData.getDataValue().compareTo(ycInde.getIndeFlag()) == 1 && ycInde.getRaiseLower() == 1 ){
                YcMeasures ycMeasures =new YcMeasures();
                ycMeasures.setDataId(ycData.getDataId());
                ycMeasures.setIndeId(ycData.getIndeId());
                ycMeasures.setMeaStatus(0);
                ycMeasuresMapper.insertYcMeasures(ycMeasures);
                YcMeasures ycMeasures1 = ycMeasuresMapper.selectYcMeasuresByDataId(ycData.getDataId());
                ycData.setStatus(3);
                ycData.setUpToStandard(1);
                ycData.setMeaId(ycMeasures1.getMeaId());
                ycDataMapper.updateYcData(ycData);
            }else {
                ycData.setStatus(3);
                ycData.setUpToStandard(0);
                ycDataMapper.updateYcData(ycData);
            }
        }
    }

    /**
     * 批量删除油厂数据管理
     *
     * @param dataIds 需要删除的油厂数据管理主键
     * @return 结果
     */
    @Override
    public int deleteYcDataByDataIds(Long[] dataIds)
    {
        return ycDataMapper.deleteYcDataByDataIds(dataIds);
    }

    /**
     * 删除油厂数据管理信息
     *
     * @param dataId 油厂数据管理主键
     * @return 结果
     */
    @Override
    public int deleteYcDataByDataId(Long dataId)
    {
        return ycDataMapper.deleteYcDataByDataId(dataId);
    }

    public List<YcData> selectYcDataYear(){return ycDataMapper.selectYcDataYear();}
    public List<YcData> selectYcDataMonth(){return ycDataMapper.selectYcDataMonth();}

    /**
     * 一键发布数据-厂
     * @param ycData 油厂数据管理
     */
    public void importDatas1(YcData ycData){
        //公共数据
        int dataDimension = ycData.getDataDimension();
        int dimension = ycData.getDimension();
        Long dataYear = ycData.getDataYear();

        //厂级数据发布
        List<YcOrg> factoryOrgs = ycOrgMapper.selectYcFactoryOrgAll();
        List<YcInde> factoryIndes = ycIndeMapper.selectYcFactoryInde(ycData);
        for(YcOrg orgId:factoryOrgs){
            for(YcInde ycInde:factoryIndes){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }


    }
    /**
     * 一键发布数据-区
     * @param ycData 油厂数据管理
     */
    public void importDatas2(YcData ycData){
        //公共数据
        int dataDimension = ycData.getDataDimension();
        int dimension = ycData.getDimension();
        Long dataYear = ycData.getDataYear();

        //区级数据发布
        List<YcOrg> districtOrgs = ycOrgMapper.selectYcDistrictOrgAll();
        List<YcInde> districtIndes = ycIndeMapper.selectYcDistrictInde(ycData);
        for(YcOrg orgId:districtOrgs){
            for(YcInde ycInde:districtIndes){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }

    }
    /**
     * 一键发布数据-站
     * @param ycData 油厂数据管理
     */
    public void importDatas3(YcData ycData){
        //公共数据
        int dataDimension = ycData.getDataDimension();
        int dimension = ycData.getDimension();
        Long dataYear = ycData.getDataYear();

        //站级数据发布
        List<YcInde> stationIndes = ycIndeMapper.selectYcStationInde(ycData);
        for (YcInde ycInde:stationIndes){
            Long deptId = ycInde.getDeptId();
            SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
            YcOrg ycOrgFather = ycOrgMapper.selectYcOrgByOrgName(sysDept.getDeptName());
            List<YcOrg> childrenOrgs = ycOrgMapper.selectChildrenOrgs(ycOrgFather.getOrgId());
            for(YcOrg orgId:childrenOrgs){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }
    }


    /**
     * 一键发布数据
     * @param ycData 油厂数据管理
     */
    public void importDatas(YcData ycData)
    {
        //公共数据
        int dataDimension = ycData.getDataDimension();
        int dimension = ycData.getDimension();
        Long dataYear = ycData.getDataYear();

        //站级数据发布
        List<YcInde> stationIndes = ycIndeMapper.selectYcStationInde(ycData);
        for (YcInde ycInde:stationIndes){
            Long deptId = ycInde.getDeptId();
            SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
            YcOrg ycOrgFather = ycOrgMapper.selectYcOrgByOrgName(sysDept.getDeptName());
            List<YcOrg> childrenOrgs = ycOrgMapper.selectChildrenOrgs(ycOrgFather.getOrgId());
            for(YcOrg orgId:childrenOrgs){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }

        //区级数据发布
        List<YcOrg> districtOrgs = ycOrgMapper.selectYcDistrictOrgAll();
        List<YcInde> districtIndes = ycIndeMapper.selectYcDistrictInde(ycData);
        for(YcOrg orgId:districtOrgs){
            for(YcInde ycInde:districtIndes){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }
        //厂级数据发布
        List<YcOrg> factoryOrgs = ycOrgMapper.selectYcFactoryOrgAll();
        List<YcInde> factoryIndes = ycIndeMapper.selectYcFactoryInde(ycData);
        for(YcOrg orgId:factoryOrgs){
            for(YcInde ycInde:factoryIndes){
                ycData.setIndeId(ycInde.getIndeId());
                ycData.setOrgId(orgId.getOrgId());
                ycData.setDataDimension(dataDimension);
                ycData.setDimension(dimension);
                ycData.setStatus(0);
                ycData.setDataYear(dataYear);
                ycData.setUpToStandard(2);
                insertYcData(ycData);
            }
        }
    }

    /**
     *   导入数据
     * @param dataList 指标列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importData(List<YcData> dataList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dataList) || dataList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(YcData ycData:dataList){
            try{
                YcData d = ycDataMapper.selectYcDataByDataId(ycData.getDataId());
                if(StringUtils.isNull(d)){
                    BeanValidators.validateWithException(validator,ycData);
                    //转换指标名称和指标编号
                    if(ycIndeMapper.selectYcInde(ycData) != null){
                        YcInde ycInde = ycIndeMapper.selectYcInde(ycData);
                        ycData.setIndeId(ycInde.getIndeId());
                    }
                    //转换组织名称和组织编号
                    if (ycOrgMapper.selectYcOrgByOrgName(ycData.getOrgIdName()) != null){
                        YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgName(ycData.getOrgIdName());
                        ycData.setOrgId(ycOrg.getOrgId());
                    }
                    //
                    this.insertYcData(ycData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");
                }else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, ycData);
                    this.updateYcData(ycData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 已存在");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum +  "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if(failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
